unit Cons_Bordero;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Formulario_Padrao_Consulta, StdCtrls, Buttons, ExtCtrls, Grids, DBGrids,
  TFlatButtonUnit;

type
  TFrmCons_Bordero = class(TFrmConsulta)
    dbgrid: TDBGrid;
    rdgOrdem: TRadioGroup;
    edtConsulta: TEdit;
    Label1: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure BtnFecharClick(Sender: TObject);
    procedure dbgridTitleClick(Column: TColumn);
    procedure edtConsultaExit(Sender: TObject);
    procedure rdgOrdemClick(Sender: TObject);
  private
    { Private declarations }
    procedure fazConsulta;
  public
    { Public declarations }

  end;

var
  FrmCons_Bordero: TFrmCons_Bordero;
  con_Tipo: string;
  xcons_bordero: string;

implementation

uses dm, dbtables, db, clsBordero;

var
  tq: TQuery;
  tds: TDataSource;
  bordero: TBordero;

{$R *.DFM}

procedure TFrmCons_Bordero.FormCreate(Sender: TObject);
begin
  inherited;
  bordero := TBordero.Create;
  tq := TQuery.Create(self);
  tds := TDataSource.Create(self);
end;

procedure TFrmCons_Bordero.FormDestroy(Sender: TObject);
begin
  inherited;
  bordero.free;
  tq.free;
  tds.free;
end;

procedure TFrmCons_Bordero.FormActivate(Sender: TObject);
begin
  inherited;
  tq.databasename := dmint.db.databasename;
  tds.dataset := tq;
  dbgrid.datasource := tds;
  //bordero.Consultar(tq);
  fazConsulta;

end;

procedure TFrmCons_Bordero.BtnFecharClick(Sender: TObject);
begin
  xcons_bordero := tq.fieldbyname('Bord_Numero').asstring;
  inherited;
end;

procedure TFrmCons_Bordero.dbgridTitleClick(Column: TColumn);
begin
  inherited;
  rdgOrdem.ItemIndex := Column.Index;
end;

procedure TFrmCons_Bordero.fazConsulta;
begin
  tq.Close;
  tq.SQL.Clear;
  tq.Params.Clear;
  tq.SQL.Add('select * from bordero ');

  if con_tipo = '' then
    con_Tipo := 'R';

  if (edtConsulta.Text <> '') then begin
    tq.SQl.Add(' where (soundex(bord_numero) = soundex(' + QuotedStr(edtConsulta.Text) + ')) or ' +
      '       (UPPER(bord_numero) like ' + QuotedStr('%' + UpperCase(edtConsulta.Text) + '%') + ') ');
    tq.SQL.Add(' and Bord_PagRec = ' + QuotedStr(con_Tipo) + ' ');
  end else
    tq.SQL.Add(' where Bord_PagRec = ' + QuotedStr(con_Tipo) + ' ');

  case rdgOrdem.ItemIndex of
    0: tq.SQL.Add('order by bord_numero ');
    1: tq.SQL.Add('order by bord_mensagem, bord_numero ');
  end;

  tq.Open;

end;

procedure TFrmCons_Bordero.edtConsultaExit(Sender: TObject);
begin
  inherited;
  fazConsulta;
end;

procedure TFrmCons_Bordero.rdgOrdemClick(Sender: TObject);
begin
  inherited;
  fazConsulta;
end;

end.
